﻿using AppSocketModel; 
using AppSocketService.Account;
using BCGPSCommon;
using BCGPSMongoData;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace AppSocketService.SendTimes
{
    public class SendTimesHistoryService : ServiceBase, ISendTimesHistoryService
    {
        private MongoDBCommand<SendTimesDto> mongoCommand = new MongoDBCommand<SendTimesDto>("CmdSendTimesHistory");
        private readonly IRedisService redisService;
        private readonly AccountDto CurrentAccount;
        public SendTimesHistoryService(IClaimsAccessor claimsAccesso, IRedisService redisService, IAccountService accountService,IAccountPersonService accountPersonService,ISystemAccountService systemAccountService ) : base(claimsAccesso)
        {
            this.redisService = redisService;
            if (claimsAccesso.AccountRole == AccountRole.SYSTEM)
                CurrentAccount = systemAccountService.GetCurrentAccount().AsAccountDto();
            else if (claimsAccesso.AccountRole == AccountRole.Company)
                CurrentAccount = accountService.GetCurrentAccount();
            else if (claimsAccesso.AccountRole == AccountRole.PERSON)
                CurrentAccount = accountPersonService.GetCurrentAccount();
        }
         

        public bool SetCmd(string imei, int times)
        {
            if (string.IsNullOrWhiteSpace(imei) || imei.Length < 15)
                throw new ArgumentException("请输入正确的IMEI编号!");
            if (times > 100 )
                throw new ArgumentException("发送次数不能大于100!");

            var dto = mongoCommand.SelectDocumentsAsync(s => s.IMEI == imei && s.CmdCodeStr == GpsCmdCode.SendTimes.ToString() && s.CmdToward == "Send" && s.CmdDateTime > DateTime.Now.AddMonths(-1)).GetAwaiter().GetResult().OrderBy(s => s.CmdDateTime).LastOrDefault();
             

            SendTimesDto sendTimesDto = new SendTimesDto();
            sendTimesDto.CmdDateTime = MongoDBDateTime.GetNowUtc();
            sendTimesDto.IMEI = imei;
            sendTimesDto.CmdCode = ((int)GpsCmdCode.SendTimes).ToString();
            sendTimesDto.CmdCodeStr = GpsCmdCode.SendTimes.ToString();
            sendTimesDto.CmdParamer = times.ToString();
            sendTimesDto.CmdToward = "Send";
            sendTimesDto.CompanyId = CurrentAccount.CompanyId;
            sendTimesDto.AccountId = CurrentAccount.AccountId;
            sendTimesDto.AccountCode = CurrentAccount.AccountCode;
            sendTimesDto.AccountRole = CurrentAccount.AccountRole;

            mongoCommand.InsertDocumentAsync(sendTimesDto).GetAwaiter().GetResult();

            redisService.AddAsync(BCGPSCommon.GeneralContainer.RedisNameSpaceIMEISendCmd + imei, "$CMD$5$" + times + "$");

            return true;
        }

        public PageableList<SendTimesDto> GetList(string imei, DateTime start, DateTime end, bool desc, int index, int size)
        {
            index = index > 0 ? index - 1 : 0;
            if(end.Hour ==0 && end.Minute ==0 && end.Second== 0) 
                end = end.AddDays(1); 
            start = MongoDBDateTime.GetUtc(start);
            end = MongoDBDateTime.GetUtc(end);
            return mongoCommand.SelectDocumentsAsync(s => s.IMEI == imei && s.CmdDateTime > start && s.CmdDateTime < end, s => s.CmdDateTime, desc, index * size, size).GetAwaiter().GetResult();
        }

    }
}
